//
//  AppointVC.swift
//  ANOA
//
//  Created by 邱仙凯 on 2024/12/17.
//  Copyright © 2024 zwb. All rights reserved.
//

import UIKit
struct PointModel {
    var taskName = ""
    var uname = ""
    var uid = ""
    var taskId = ""
    var nodeId = ""
}

 
class AppointVC: GlobalFormVC {
 
    var item1 :KSFormHItem = {
        let appe = KSFormHAppear()
            .roundType(.top)
            .placeholder("")
         let v = KSFormHItem(title: "改单节点",appearance:appe )
          return v
    }()
    var item2 :KSFormHItem = {
        let appe = KSFormHAppear()
            .contentHight(200)
         let v = KSFormHItem(title: "改单意见",appearance:appe )
          return v
    }()
    
    // 无细表时可以用这保存控件，直接添加到header
    lazy var appointButton: KSUIButton = {
        let button = KSUIButton(titlex: "提交指定")
         return button
    }()
    var holdPprogress = [TodopProgress]()
    
    
    var showPprogress = [PointModel]()

    var selfmodel = ToDoModel(JSON:["":""])!
    var selectedOne = PointModel()
    var mmemo = ""
    var appointSuccess:(()->())?
    override func viewDidLoad() {
        super.viewDidLoad()
        title = "指定改单"
        
        
        for i in 0..<self.holdPprogress.count {
            let oneqq = self.holdPprogress[i]
            if oneqq.result.unwrap != "" {
                if oneqq.userinfo.count > 0 {
                    for item in oneqq.userinfo {
                        
                        let mmm = PointModel(taskName: oneqq.title.unwrap,uname: item.user.unwrap,uid: item.userid.unwrap,taskId: oneqq.taskId.unwrap,nodeId: oneqq.nodeId.unwrap)
                        showPprogress.append(mmm)
                    }

                } else {
                    let mmm = PointModel(taskName: oneqq.title.unwrap,uname: oneqq.user.unwrap,uid: oneqq.userid.unwrap,taskId: oneqq.taskId.unwrap,nodeId: oneqq.nodeId.unwrap)
                    showPprogress.append(mmm)
                }
            }
            
        }
        
        
        
         self.configView()
        self.configAction()
        self.configDataSource()
         
    }
    
    func configView() {
      
        bg.add(item1)
            .add(item2).add(appointButton)
           
         header.ks_height = bg.frame.maxY
    }
    func configAction() {
        item2.textValueChangeClo = {str in
            self.mmemo = str
        }
        item1.actionClosure = {
            var allArr = [nameAndModel<PointModel>]()
            for i in 0..<self.showPprogress.count {
                let oneqq = self.showPprogress[i]
          
                
                
                let one_x = nameAndModel(shows: [oneqq.taskName,oneqq.uname], model: oneqq)
                allArr.append(one_x)
            }
            let cle:(nameAndModel<PointModel>)->() = { one in
                self.selectedOne = one.model
                 self.configDataSource()
            }
            
            let vc = KSSingleSelectUtil.initWithNav(title: "选择", allArr: allArr, backClosure: cle)
            self.present_FullScreen(to: vc)
        }
        appointButton.button.addTouchUpInSideAction { _ in
            KSNetwork.shared.doRequest(API.auditFlow(.sendModifyVou(instanceId: self.selfmodel.instanceId.unwrap, taskId: self.selectedOne.taskId, nodeId: self.selectedOne.nodeId, task_name: self.selectedOne.taskName, staff_id: self.selectedOne.uid, staff_nm: self.selectedOne.uname, m_memo: self.mmemo, curr_task_id: self.selfmodel.taskId.unwrap, curr_node_id: self.selfmodel.nodeId.unwrap))) { (data:BaseResult<OperateResult>) in
                
                if data.flag == TRUE {
                    KSToast("指定成功")
                    self.navigationController?.popViewController(animated: true)
                    if let clo = self.appointSuccess {
                        clo()
                    }
                } else {
                    KSToast(data.errorCombine.unwrap)
                }
            }
        }
    }

    func configDataSource() {
        if selectedOne.uname != "" {
            item1.setVal(selectedOne.taskName + "-" + selectedOne.uname)

        } else {
            item1.setVal("")

        }
     }
  
    
}
